"""
系统配置相关数据模型
"""

from tortoise import fields
from .base import BaseModel


class SystemConfig(BaseModel):
    """
    系统配置模型
    """
    key = fields.CharField(max_length=100, unique=True, description="配置键")
    value = fields.TextField(description="配置值")
    description = fields.CharField(max_length=255, null=True, description="配置描述")
    config_type = fields.CharField(max_length=50, default="string", description="配置类型")
    is_public = fields.BooleanField(default=False, description="是否为公开配置")
    
    class Meta:
        table = "system_configs"
        table_description = "系统配置表"
        
    def __str__(self):
        return f"SystemConfig(key={self.key}, value={self.value})"
    
    @classmethod
    async def get_config(cls, key: str, default=None):
        """获取配置值"""
        try:
            config = await cls.get(key=key, is_deleted=False)
            return config.value
        except:
            return default
    
    @classmethod
    async def set_config(cls, key: str, value: str, description: str = None, config_type: str = "string"):
        """设置配置值"""
        config, created = await cls.get_or_create(
            key=key,
            defaults={
                "value": value,
                "description": description,
                "config_type": config_type
            }
        )
        if not created:
            config.value = value
            if description:
                config.description = description
            config.config_type = config_type
            await config.save()
        return config


class OperationLog(BaseModel):
    """
    操作日志模型
    """
    user = fields.ForeignKeyField('models.User', related_name='operation_logs', null=True, description="操作用户")
    action = fields.CharField(max_length=50, description="操作动作")
    resource = fields.CharField(max_length=100, description="操作资源")
    resource_id = fields.CharField(max_length=50, null=True, description="资源ID")
    ip_address = fields.CharField(max_length=45, null=True, description="操作IP地址")
    user_agent = fields.TextField(null=True, description="用户代理")
    request_data = fields.JSONField(null=True, description="请求数据")
    response_data = fields.JSONField(null=True, description="响应数据")
    status_code = fields.IntField(null=True, description="响应状态码")
    execution_time = fields.FloatField(null=True, description="执行时间(毫秒)")
    
    class Meta:
        table = "operation_logs"
        table_description = "操作日志表"
        
    def __str__(self):
        return f"OperationLog(action={self.action}, resource={self.resource})"


class DatabaseInitLog(BaseModel):
    """
    数据库初始化日志模型
    """
    version = fields.CharField(max_length=50, description="数据库版本")
    init_type = fields.CharField(max_length=50, description="初始化类型")
    status = fields.CharField(max_length=20, description="状态")
    message = fields.TextField(null=True, description="初始化消息")
    execution_time = fields.FloatField(null=True, description="执行时间(秒)")
    
    class Meta:
        table = "database_init_logs"
        table_description = "数据库初始化日志表"
        
    def __str__(self):
        return f"DatabaseInitLog(version={self.version}, status={self.status})"